#install.packages("data.table")
#install.packages("tidyr")
#install.packages("reshape")
#install.packages("ggplot2")
#install.packages("lme4")
#install.packages("reshape")
library(data.table)
library(lme4)

#trust gage
# zeptat se jakto�e nejsou u n�kter�ch t�ch random stran informace o tom, kter� je to strana
# jak se m��� �as, je mozne, ze je zmereny spatne?
# jaktoze nekter� dotazn�ky s progressem 100 jsou zcela nevypln�n�
# 400 lid� v�bec neza�alo hru hr�t, ae v�ichni kdo zahr�li prvn� kolo do�li a� na konec, to je docela divn�

# m�me 946 kompletn�ch p��pad�, tak ty v�hy ud�l�m jenom pro n�

trustgame <- read.csv("C:\\Users\\petrh\\Downloads\\trustgame\\trustgame.csv", sep=";")
vaha_pohlavi <- read.csv("C:\\Users\\petrh\\Downloads\\trustgame\\vaha_pohlavi.csv", sep=";")
vaha_vek <- read.csv("C:\\Users\\petrh\\Downloads\\trustgame\\vaha_vek.csv", sep=";")
vaha_vzdel <- read.csv("C:\\Users\\petrh\\Downloads\\trustgame\\vaha_vzdel.csv", sep=";")
vaha_obec <- read.csv("C:\\Users\\petrh\\Downloads\\trustgame\\vaha_obec.csv", sep=";")
vaha_kraj <- read.csv("C:\\Users\\petrh\\Downloads\\trustgame\\vaha_kraj.csv", sep=";")


trustgame_w <- merge(trustgame, vaha_pohlavi, by.x="Gender", by.y="pohlavi", all.x=T)
trustgame_w <- merge(trustgame_w, vaha_vek, by.x="Age", by.y="vek", all.x=T)
trustgame_w <- merge(trustgame_w, vaha_vzdel, by.x="Education", by.y="vzdelani", all.x=T)
trustgame_w <- merge(trustgame_w, vaha_obec, by.x="Settlement", by.y="obec", all.x=T)
trustgame_w <- merge(trustgame_w, vaha_kraj, by.x="Region", by.y="kraj", all.x=T)


names(trustgame_w)

trustgame_w$weight = trustgame_w$vaha_pohlavi*trustgame_w$vaha_vek*trustgame_w$vaha_vzdel*trustgame_w$vaha_obec*trustgame_w$vaha_kraj
summary(trustgame_w)

#zjistit, jak p�ed�lat na dlouh� form�t

t <- reshape(trustgame_w, 
        direction = "long",
        varying = list(names(trustgame_w)[34:42]),
        v.names = "Value",
        idvar = c("Region","Settlement","Education","Age","Gender", "PartyPref","PartyNeg","LeftRight_1",
                  "AttImmigr","SaliImmigr","Interest", "Sympathy_1","Sympathy_2","Sympathy_3","Sympathy_4","Sympathy_5","Sympathy_6",
                  "Sympathy_7","Sympathy_8","Sympathy_9" ),
        timevar = "treatment",
        times = c("Control","PartyPositive","PartyNegative",
"PartyRandom","ImigrPro","ImigrNeutral","ImigrAnti","PlaceboIngroup","PlaceboOutgroup"))

names(t)
table(trustgame$nezakliknuto)
table(t$Value)
table(t$treatment)
t$ImigrPro <- 0
t$ImigrPro[t$treatment== "ImigrPro"] <- 1
t$ImigrAnti <- 0
t$ImigrAnti[t$treatment== "ImigrAnti"] <- 1
t$ImigrNeutral <- 0
t$ImigrNeutral[t$treatment== "ImigrNeutral"] <- 1
t$PartyNegative <- 0
t$PartyNegative[t$treatment== "PartyNegative"] <- 1
t$PartyPositive <- 0
t$PartyPositive[t$treatment== "PartyPositive"& t$PartyPref != 10] <- 1
t$nonvoter <- 0
t$nonvoter[t$treatment== "PartyPositive" & t$PartyPref == 10] <- 1
t$PartyRandom <- 0
t$PartyRandom[t$treatment== "PartyRandom"] <- 1
t$PlaceboIngroup <- 0
t$PlaceboIngroup[t$treatment== "PlaceboIngroup"] <- 1
t$PlaceboOutgroup <- 0
t$PlaceboOutgroup[t$treatment== "PlaceboOutgroup"] <- 1
table(t$PartyPref, t$PartyPositive)

table(t$treatment)


t$nonvoter_match <- 0
t$nonvoter_match[t$PartyPref=="10" & t$nezakliknuto == "��dn� strana"] <- 1
t$nonvoter_match[t$treatment== "PartyPositive" & t$PartyPref == 10] <- 1

t$centreleft_match <-0
t$centreleft_match[(t$PartyPref==1 )& t$treatment== "PartyRandom"&( t$nezakliknuto == "�SSD"| t$nezakliknuto == "KS�M")] <- 1
t$centreleft_match[(t$PartyPref == 5)&t$treatment== "PartyRandom"& (t$nezakliknuto == "ANO" | t$nezakliknuto == "KS�M")] <- 1
t$centreleft_match[(t$PartyPref == 8)& t$treatment== "PartyRandom"&(t$nezakliknuto == "ANO" | t$nezakliknuto == "�SSD")] <- 1
t$centreleft_match[(t$PartyPref==1 )& t$treatment== "PartyNegative"&( t$PartyNeg == 5| t$PartyNeg == 8)] <- 1
t$centreleft_match[(t$PartyPref == 5)&t$treatment== "PartyNegative"& (t$PartyNeg == 1 | t$PartyNeg == 8)] <- 1
t$centreleft_match[(t$PartyPref == 8)& t$treatment== "PartyNegative"&(t$PartyNeg == 1 | t$PartyNeg == 5)] <- 1

t$centreright_match <-0
t$centreright_match[(t$PartyPref==7) &t$treatment== "PartyRandom"& (t$nezakliknuto=="ODS" |t$nezakliknuto=="Pir�ti" | t$nezakliknuto=="STAN" | t$nezakliknuto=="TOP 09" )] <- 1
t$centreright_match[(t$PartyPref==2) & t$treatment== "PartyRandom"&(t$nezakliknuto == "KDU-�SL" |t$nezakliknuto=="Pir�ti" | t$nezakliknuto=="STAN" | t$nezakliknuto=="TOP 09" )] <- 1
t$centreright_match[(t$PartyPref==3) & t$treatment== "PartyRandom"&(t$nezakliknuto == "KDU-�SL"|t$nezakliknuto=="ODS"  | t$nezakliknuto=="STAN" | t$nezakliknuto=="TOP 09" )] <- 1
t$centreright_match[(t$PartyPref==6) & t$treatment== "PartyRandom"&(t$nezakliknuto == "KDU-�SL"|t$nezakliknuto=="ODS" |t$nezakliknuto=="Pir�ti" | t$nezakliknuto=="TOP 09" )] <- 1
t$centreright_match[(t$PartyPref==9)  & t$treatment== "PartyRandom"&(t$nezakliknuto == "KDU-�SL"|t$nezakliknuto=="ODS" |t$nezakliknuto=="Pir�ti" | t$nezakliknuto=="STAN" )] <- 1
t$centreright_match[(t$PartyPref==7) &t$treatment== "PartyNegative"& (t$PartyNeg==2 |t$PartyNeg==3 | t$PartyNeg==6 | t$PartyNeg==9 )] <- 1
t$centreright_match[(t$PartyPref==2) & t$treatment== "PartyNegative"&(t$PartyNeg == 7 |t$PartyNeg==3 | t$PartyNeg==6 | t$PartyNeg==9 )] <- 1
t$centreright_match[(t$PartyPref==3) & t$treatment== "PartyNegative"&(t$PartyNeg == 7|t$PartyNeg==2  | t$PartyNeg==6 | t$PartyNeg==9 )] <- 1
t$centreright_match[(t$PartyPref==6) & t$treatment== "PartyNegative"&(t$PartyNeg == 7|t$PartyNeg==2 |t$PartyNeg==3 | t$PartyNeg==9 )] <- 1
t$centreright_match[(t$PartyPref==9)  & t$treatment== "PartyNegative"&(t$PartyNeg == 7|t$PartyNeg==2 |t$PartyNeg==3 | t$PartyNeg==6 )] <- 1

t$centreleft_SPD_mismatch <-0
t$centreleft_SPD_mismatch[(t$PartyPref==1 | t$PartyPref == 5| t$PartyPref == 8)&t$treatment== "PartyRandom"& (t$nezakliknuto == "SPD")] <- 1
t$centreleft_SPD_mismatch[(t$PartyPref==1 | t$PartyPref == 5| t$PartyPref == 8)&t$treatment== "PartyNegative"& (t$PartyNeg == 4)] <- 1
t$centreright_SPD_mismatch <-0
t$centreright_SPD_mismatch[(t$PartyPref==7 |t$PartyPref==2 |t$PartyPref==3 | t$PartyPref==6 | t$PartyPref==9 ) &t$treatment== "PartyRandom"& (t$nezakliknuto == "SPD" )] <- 1
t$centreright_SPD_mismatch[(t$PartyPref==7 |t$PartyPref==2 |t$PartyPref==3 | t$PartyPref==6 | t$PartyPref==9 ) &t$treatment== "PartyNegative"& (t$PartyNeg == 4 )] <- 1
t$SPD_centreleft_mismatch <-0
t$SPD_centreleft_mismatch[t$PartyPref==4 & t$treatment== "PartyRandom"&(t$nezakliknuto == "ANO" | t$nezakliknuto == "�SSD"| t$nezakliknuto == "KS�M")] <- 1
t$SPD_centreleft_mismatch[t$PartyPref==4 & t$treatment== "PartyNegative"&(t$PartyNeg == 1 | t$PartyNeg == 5| t$PartyNeg == 8)] <- 1
t$SPD_centreright_mismatch <-0
t$SPD_centreright_mismatch[t$PartyPref==4 & t$treatment== "PartyRandom"&(t$nezakliknuto == "KDU-�SL"|t$nezakliknuto=="ODS" |t$nezakliknuto=="Pir�ti" | t$nezakliknuto=="STAN" | t$nezakliknuto=="TOP 09" )] <- 1
t$SPD_centreright_mismatch[t$PartyPref==4 & t$treatment== "PartyNegative"&(t$PartyNeg == 7|t$PartyNeg==2 |t$PartyNeg==3 | t$PartyNeg==6 | t$PartyNeg==9 )] <- 1
t$centreleft_centreright_mismatch <-0
t$centreleft_centreright_mismatch[(t$PartyPref==1 | t$PartyPref == "�SSD"| t$PartyPref == "KS�M")&t$treatment== "PartyRandom"& (t$nezakliknuto == "KDU-�SL"|t$nezakliknuto=="ODS" |t$nezakliknuto=="Pir�ti" | t$nezakliknuto=="STAN" | t$nezakliknuto=="TOP 09" )] <- 1
t$centreleft_centreright_mismatch[(t$PartyPref==1 | t$PartyPref == 5| t$PartyPref == 8)&t$treatment== "PartyNegative"& (t$PartyNeg == 7|t$PartyNeg==2 |t$PartyNeg==3 | t$PartyNeg==6 | t$PartyNeg==9 )] <- 1

t$centreright_centreleft_mismatch <-0
t$centreright_centreleft_mismatch[(t$PartyPref==7 |t$PartyPref==2 |t$PartyPref==3 | t$PartyPref==6 | t$PartyPref==9 ) & t$treatment== "PartyRandom"& (t$nezakliknuto == "ANO" | t$nezakliknuto == "�SSD"| t$nezakliknuto == "KS�M")] <- 1
t$centreright_centreleft_mismatch[(t$PartyPref==7 |t$PartyPref==2 |t$PartyPref==3 | t$PartyPref==6 | t$PartyPref==9 ) & t$treatment== "PartyNegative"& (t$PartyNeg == 1 | t$PartyNeg == 5| t$PartyNeg == 8)] <- 1






#zakodovat match a dismatch
##strany jsou ok
## imigrace
t$imigr_neg_neg <- 0
t$imigr_neg_neut <- 0
t$imigr_neg_pos <- 0
t$imigr_pos_neg <- 0
t$imigr_pos_neut <- 0
t$imigr_pos_pos <- 0
t$imigr_neut_neut <- 0
t$imigr_neut_neg <- 0
t$imigr_neut_pos <- 0
table(t$AttImmigr)
t$imigr_neg_neg[t$AttImmigr ==1 & t$ImigrAnti ==1] <- 1
t$imigr_neg_pos[t$AttImmigr ==3 & t$ImigrAnti ==1] <- 1
t$imigr_neg_neut[t$AttImmigr ==2 & t$ImigrAnti ==1] <- 1

t$imigr_pos_neg[t$AttImmigr ==1 & t$ImigrPro ==1] <- 1
t$imigr_pos_pos[t$AttImmigr ==3 & t$ImigrPro ==1] <- 1
t$imigr_pos_neut[t$AttImmigr ==2 & t$ImigrPro ==1] <- 1

t$imigr_neut_neg[t$AttImmigr ==1 & t$ImigrNeutral ==1] <- 1
t$imigr_neut_pos[t$AttImmigr ==3 & t$ImigrNeutral ==1] <- 1
t$imigr_neut_neut[t$AttImmigr ==2 & t$ImigrNeutral ==1] <- 1

table(t$imigr_neg_neg)
table(t$imigr_neg_pos)
table(t$imigr_pos_neg)
table(t$imigr_pos_pos)
model1 <- lmer(Value  ~ imigr_neg_neg + imigr_neg_neut +imigr_neg_pos +imigr_neut_neg+imigr_neut_pos+imigr_neut_neut +imigr_pos_neg + imigr_pos_neut +imigr_pos_pos +PartyNegative+PartyPositive+PartyRandom+PlaceboIngroup +PlaceboOutgroup +(1|ResponseId), data=t, weights=weight)
summary(model1)


model2 <- lmer(Value  ~ ImigrAnti+ImigrNeutral+ImigrPro+PartyNegative+PartyPositive+PartyRandom+PlaceboIngroup +PlaceboOutgroup +(1|ResponseId), data=t)
summary(model2)

t$sympathy <- NA
for(i in 1:8514){ if(t$nezakliknuto[i]=="ANO"){ t$sympathy[i] <- t$Sympathy_1[i]  } }
for(i in 1:8514){ if(t$nezakliknuto[i]=="�SSD"){ t$sympathy[i] <- t$Sympathy_4[i]  } }
for(i in 1:8514){ if(t$nezakliknuto[i]=="KDU-�SL"){ t$sympathy[i] <- t$Sympathy_6[i]  } }
for(i in 1:8514){ if(t$nezakliknuto[i]=="KS�M"){ t$sympathy[i] <- t$Sympathy_5[i]  } }
for(i in 1:8514){ if(t$nezakliknuto[i]=="ODS"){ t$sympathy[i] <- t$Sympathy_3[i]  } }
for(i in 1:8514){ if(t$nezakliknuto[i]=="Pir�ti"){ t$sympathy[i] <- t$Sympathy_2[i]  } }
for(i in 1:8514){ if(t$nezakliknuto[i]=="SPD"){ t$sympathy[i] <- t$Sympathy_7[i]  } }
for(i in 1:8514){ if(t$nezakliknuto[i]=="STAN"){ t$sympathy[i] <- t$Sympathy_8[i]  } }
for(i in 1:8514){ if(t$nezakliknuto[i]=="TOP 09"){ t$sympathy[i] <- t$Sympathy_9[i]  } }


model3 <- lmer(Value  ~ imigr_neg_neg + imigr_neg_neut +imigr_neg_pos +imigr_neut_neg+imigr_neut_pos+imigr_neut_neut +imigr_pos_neg + imigr_pos_neut +imigr_pos_pos +PartyNegative+PartyPositive+PartyRandom*sympathy+PlaceboIngroup +PlaceboOutgroup +(1|ResponseId), data=t, weights=weight)
summary(model3)


table(t$sympathy)
table(t$Sympathy_1)
table(t$nezakliknuto)

names(t)
#H1a. Player 2 being a supporter of the same party as Player 1 increases the amount of money allocated to Player 2 by Player 1 in a trust game, all else equal.
#H1b. Player 2 being a supporter of the Player 1�s least preferred party decreases the amount of money allocated to Player 2 by Player 1 in a trust game, all else equal.
#H1c. Player 2 being a supporter of any other party (apart from Player 1�s most and least preferred party)  changes the amount of money allocated 
#to Player 2 by Player 1 in a trust game depending on the level of sympathy Player 1 feels towards Player 2�s preferred party, all else equal.

#H2a. Player 2 having the same attitude on immigration as Player 1 increases the amount of money allocated to Player 2 by Player 1 in a trust game, all else equal. 
#H2b. Player 2 having the opposite attitude towards immigration than Player 1 decreases the amount of money allocated to Player 2 by Player 1 in a trust game, all else equal. 

#H3a. Player 2 being a member of a same random nonpolitical group as Player 1 will not have effect on the amount of money allocated to Player 2 by Player one in a trust game, all else equal. 
#H3b. Player 2 being a member of a different random nonpolitical group as Player 1 will not have effect on the amount of money allocated to Player 2 by Player one in a trust game, all else equal.

# H3a. Player 2 being a member of a same random nonpolitical group as Player 1 will not have effect on the amount of money allocated to Player2 by Player 1 
#in a trust game compared to congruence in party preference or congruence in attitude towards immigration.
#H3b. Player 2 being a member of a different random nonpolitical group as Player 1 will not have effect on the amount of money allocated to Player 2 by Player one 
#in a trust game compared to Payer 2�s preference of Player 1�s least preferred political party or having opposing attitude towards immigration then Player 1.



#ud�lat anal�zu jako negative binomial regression a jako tobit

t$obs_effect<-1:nrow(t)

model1 <- lmer(Value  ~ imigr_neg_neg + imigr_neg_neut +imigr_neg_pos +imigr_neut_neg+imigr_neut_pos+imigr_neut_neut +imigr_pos_neg + imigr_pos_neut +imigr_pos_pos +PartyNegative+PartyPositive+PartyRandom+PlaceboIngroup +PlaceboOutgroup +(1|ResponseId), data=t, weights=weight)
summary(model1)

hist(t$Value)
t$Value[t$Value==-1] <- 0
nullmodel <- glmer(Value ~ 1 +(1|obs_effect)+ (1| ResponseId),  
                   data=t, family=poisson,control=glmerControl(optimizer="bobyqa", optCtrl=list(maxfun=2e5)))
summary(nullmodel)

nbrmodel <- glmer(Value ~ imigr_neg_neg + imigr_neg_neut +imigr_neg_pos +imigr_neut_neg+imigr_neut_pos+imigr_neut_neut +imigr_pos_neg + imigr_pos_neut +
                          imigr_pos_pos +PartyNegative+PartyPositive+PartyRandom+PlaceboIngroup +PlaceboOutgroup  +(1|obs_effect)+ (1| ResponseId),  
                   data=t, weights=weight, family=poisson,control=glmerControl(optimizer="bobyqa", optCtrl=list(maxfun=2e5)))
summary(nbrmodel)


nbrmodel2 <- glmer(Value ~ imigr_neg_neg + imigr_neg_neut +imigr_neg_pos +imigr_neut_neg+imigr_neut_pos+imigr_neut_neut +imigr_pos_neg + imigr_pos_neut +
                          imigr_pos_pos +PartyNegative+PartyPositive+PartyRandom*sympathy+PlaceboIngroup +PlaceboOutgroup  +(1|obs_effect)+ (1| ResponseId),  
                  data=t, weights=weight, family=poisson,control=glmerControl(optimizer="bobyqa", optCtrl=list(maxfun=2e5)))
summary(nbrmodel2)

write.csv(t, "C:\\Users\\petrh\\Downloads\\trustgame\\trust_stata.csv")

hist(t$Value)
. metobit Value imigr_neg_neg  imigr_neg_neut imigr_neg_pos imigr_neut_neg imigr_neut_pos imigr_neut_neut imigr_pos_neg  imigr_pos_neut  imigr_pos_pos PartyNegative PartyPositive PartyRandom PlaceboIngroup PlaceboOutgroup || ResponseId,  ll(0) ul(400)

. metobit value imigr_neg_neg  imigr_neg_neut imigr_neg_pos imigr_neut_neg imigr_neut_pos imigr_neut_neut imigr_pos_neg  imigr_pos_neut  imigr_pos_pos partynegative partypositive partyrandom placeboingroup placebooutgroup || responseid:,  ll(0) ul(100)


. metobit value partynegative  || responseid:,  ll(0) ul(100)


summary(t)



strany <- read.csv("C:\\Users\\petrh\\Downloads\\trustgame\\trustgame_strany.csv", sep=";")
blok <- read.csv("C:\\Users\\petrh\\Downloads\\trustgame\\trutgame_blok.csv", sep=";")



p1 <- ggplot(strany, aes(match, fit))+
        geom_point(aes(x=match, y=fit), size=2, lwd=0.3)+
        geom_linerange(aes(x=match, ymin=lower, ymax=upper))+
        geom_hline(yintercept =  0, linetype="longdash")+
        coord_flip()+
        facet_grid(rows=vars(level), scales = "free_y", switch="y", space="free_y")+
        labs(title="",x="", y="difference in allocated amount")+
        theme_bw()+
        theme(strip.placement = "outside")+
    theme(strip.text.y = element_text(size=18),
          axis.text = element_text(size = 18),
          axis.title = element_text(size = 18))
p1
png(file="C:\\Users\\petrh\\Downloads\\trustgame\\figure_party_t.png",
    width=1200, height=1800)
p1
dev.off()

p1 <- ggplot(blok, aes(variable, fit))+
        geom_point(aes(x=variable, y=fit), size=2, lwd=0.3)+
        geom_linerange(aes(x=variable, ymin=lower, ymax=upper))+
        geom_hline(yintercept =  0, linetype="longdash")+
        coord_flip()+
        facet_grid(rows=vars(level), scales = "free_y", switch="y", space="free_y")+
        labs(title="",x="", y="difference in allocated amount")+
        theme_bw()+
        theme(strip.placement = "outside")+
    theme(strip.text.y = element_text(size=18),
          axis.text = element_text(size = 18),
          axis.title = element_text(size = 18))
p1
png(file="C:\\Users\\petrh\\Downloads\\trustgame\\figure_partyblok_t.png",
    width=1200, height=1800)
p1
dev.off()
blok <- read.csv("C:\\Users\\petrh\\Downloads\\trustgame\\figure2.csv", sep=";")
library(ggplot2)


p1 <- ggplot(blok, aes(variable, fit))+
        geom_point(aes(x=variable, y=fit), size=2, lwd=0.3)+
        geom_linerange(aes(x=variable, ymin=lower, ymax=upper))+
        geom_hline(yintercept =  0, linetype="longdash")+
        coord_flip()+
        facet_grid(rows=vars(level), scales = "free_y", switch="y", space="free_y")+
        labs(title="",x="", y="difference in allocated amount")+
        theme_bw()+
        theme(strip.placement = "outside")+
    theme(strip.text.y = element_text(size=18),
          axis.text = element_text(size = 18),
          axis.title = element_text(size = 18))

p1
png(file="C:\\Users\\petrh\\Downloads\\trustgame\\figure2.png",
    width=1200, height=1800)
p1
dev.off()
